import org.apache.commons.lang3.StringUtils;

public class Db38 {

  // 打印sql
  public static void main(String[] args) {
    Db38 db2 = new Db38();
    String sql = db2.getSql(null, null, null, null, null, null, null, null);
    System.out.println(sql);
  }

  /**
   * 获得要执行的SQL脚本
   *
   * @param
   * @return 需要执行的SQL脚本
   */
  //    @Override
  //    public String getSql(DbNode node, Map<String, NodeParam> binds){
  //        JSONObject in = getIn(binds);
  //        String xzqmc = in.getString("xzqmc");
  //        String jzmc = in.getString("jzmc");
  //        String czcmc = in.getString("czcmc");
  //        String wgmc = in.getString("wgmc");
  //        String xzqdm = in.getString("xzqdm");
  //        String jzdm = in.getString("jzdm");
  //        String czcdm = in.getString("czcdm");
  //        String wgdm = in.getString("wgdm");
  //        return getSql(xzqmc,jzmc,czcmc,wgmc,xzqdm,jzdm,czcdm,wgdm);
  //    }

  public String getSql(
      String xzqmc,
      String jzmc,
      String czcmc,
      String wgmc,
      String xzqdm,
      String jzdm,
      String czcdm,
      String wgdm) {
    String sql0 = " select * from ( \n";

    String sql1 =
        "select 1 as type,YEAR (now()) - YEAR (substring(zjhm, 7, 8)) age,JYLX AS JXQK,ETLB AS ETLX,''  AS ETZT,JYLX AS JHLX,ZJHM AS SFZHM,XM,CZCMC,CZCDM,WGMC,WGDM,XZQMC,XZQDM from ads_czc_dw_gdsgexx_czc\n"
            + "where 1=1  \n"
            + getSearchSql("xzqmc", xzqmc)
            + getSearchSql("jzmc", jzmc)
            + getSearchSql("czcmc", czcmc)
            + getSearchSql("wgmc", wgmc)
            + getSearchSql("xzqdm", xzqdm)
            + getSearchSql("jzdm", jzdm)
            + getSearchSql("czcdm", czcdm)
            + getSearchSql("wgdm", wgdm);
    String sql2 = " \n union all \n";
    String sql3 =
        " select 2 as type,YEAR (now()) - YEAR (substring(sfzhm, 7, 8)) age,JXQK,ETLX,ETZT,JHLX,SFZHM,XM,CZCMC,CZCDM,WGMC,WGDM,XZQMC,XZQDM from ads_dw_kjetxx_czc  "
            + "where 1=1  \n"
            + getSearchSql("xzqmc", xzqmc)
            + getSearchSql("jzmc", jzmc)
            + getSearchSql("czcmc", czcmc)
            + getSearchSql("wgmc", wgmc)
            + getSearchSql("xzqdm", xzqdm)
            + getSearchSql("jzdm", jzdm)
            + getSearchSql("czcdm", czcdm)
            + getSearchSql("wgdm", wgdm);

    String sql00 = " ) t order by type desc";
    return sql0 + sql1 + sql2 + sql3 + sql00;
  }

  // 获取前端传过来的入参
  //    public static JSONObject getIn(Map<String, NodeParam> params){
  //        NodeParam input = params.get("input");
  //        JSONObject json = (JSONObject) JSON.toJSON(input.getIn());
  //        return json;
  //    }
  //
  //
  //
  //
  public String getSearchSql(String column, String value) {
    String sql = " and 1=1 ";
    if (StringUtils.isNotBlank(value)) {
      if ("其他".equals(value)) {
        sql = " and " + column + " is null ";
      } else {
        sql = " and " + column + "  =  " + "'" + value + "'";
      }
    }
    return sql;
  }
}
